/**
 * @fileoverview
 * TODO doc me
 */


// Shortcuts
var log = YAHOO.log;
var assert = YAHOO.util.Assert;


// ************************************************************
// MODULE TESTS
// ************************************************************


/**
 * TODO declare the class
 */
flyui.bdgp.Service.ModuleTests = function(){};


/** 
 * Test that the contents of the flyui.bdgp.Service module have been defined.
 */
flyui.bdgp.Service.ModuleTests.testModuleContents = function() {
	
	log("Test \"==== flyui.bdgp.Service ModuleTests :: testModuleContents ====\" started.");
	
	assert.isNotUndefined(flyui.bdgp.Service, "flyui.bdgp.Service is undefined");	
	
	assert.isNotUndefined(flyui.bdgp.Image, "flyui.bdgp.Image is undefined");
	
};


/**
 * TODO
 */
flyui.bdgp.Service.ModuleTestCase = function (){
	var testCase = new YAHOO.tool.TestCase({
		
		name: "=== flyui.bdgp.Service ModuleTests ===",
		
		testModuleContents : flyui.bdgp.Service.ModuleTests.testModuleContents
		
	});
	
	return testCase;
}



// ************************************************************
// SERVICE TESTS
// ************************************************************


/**
 * TODO
 */
flyui.bdgp.Service.ServiceTests = function(){};


/**
 * TODO doc me
 */
flyui.bdgp.Service.ServiceTests.testQuery = function(endpointURL, testCase){
	log("Test \"==== flyui.bdgp.Service ServiceTests :: testQuery ====\" started.");
		
	var service = new flyui.bdgp.Service(endpointURL);
	var query = "ASK { ?s ?p ?o }";
	
	var testOnSuccess = function(responseObject) {
		log("resume test case (success)", "test");
		testCase.resume(function() {
			
			// check status code			
			assert.areEqual(200, responseObject.status);
			
			// try parsing response text as json
			var resultSet = YAHOO.lang.JSON.parse(responseObject.responseText);
			
			// check json content
			assert.isTrue(resultSet["boolean"], "result set not as expected");
		});
	};
	
	var testOnFailure = function(responseObject) {
		log("resume test case (failure)", "test");
		testCase.resume(function() {
			// force failure
			assert.fail("request failed, status "+responseObject.status + " " +responseObject.statusText);
		});
	};
	
	log("initiate request", "test");
	service.query(query, testOnSuccess, testOnFailure);
	
	log("suspend test case (if test runner hangs here, something is wrong)", "test");
	testCase.wait();
};



/**
 * TODO doc me
 * @static
 * @asynchronous
 */
flyui.bdgp.Service.ServiceTests.testFindImagesByAnyGeneLabel = function( geneLabel, expectedImages, endpointURL, testCase ) {
	
	log("Test \"==== flyui.bdgp.Service ServiceTests :: testFindImagesByAnyGeneLabel ("+geneLabel+") ====\" started.");
	
	var service = new flyui.bdgp.Service(endpointURL);
	
	var testOnSuccess = function( images ) {
		log("resume test case (success)", "test");
		testCase.resume(function() {

			flyui.bdgp.Service.TestData.compareImageArrays(expectedImages, images);
			
		});
	};

	var testOnFailure = function( response ) {
		log("resume test case (failure)", "test");
		testCase.resume(function() {
			assert.fail("request failed, "+response.status+" "+response.statusText);			
		});
	};

	log("initiate request", "test");
	service.findImagesByAnyGeneLabel(geneLabel, testOnSuccess, testOnFailure);
	
	log("suspend test case (if test runner hangs here, something is wrong)", "test");
	testCase.wait();
	
}

/**
 * TODO doc me
 */
flyui.bdgp.Service.ServiceTestCase = function (endpointURL){
	var testCase = new YAHOO.tool.TestCase({
		name: "=== flyui.bdgp.Service ServiceTests ===",
		
		testQuery : function() {
			flyui.bdgp.Service.ServiceTests.testQuery(endpointURL, this);
		},
		
		testFindImagesByAnyGeneLabel_foo : function() {
			var geneLabel = "foo";
			var testData = new flyui.bdgp.Service.TestData();
			var expectedImages = [];
			flyui.bdgp.Service.ServiceTests.testFindImagesByAnyGeneLabel(geneLabel, expectedImages, endpointURL, this);
		},

		testFindImagesByAnyGeneLabel_CG10561 : function() {
			var geneLabel = "CG10561";
			var testData = new flyui.bdgp.Service.TestData();
			var expectedImages = [testData.img_CG10561];
			flyui.bdgp.Service.ServiceTests.testFindImagesByAnyGeneLabel(geneLabel, expectedImages, endpointURL, this);
		},

		testFindImagesByAnyGeneLabel_CG12750 : function() {
			var geneLabel = "CG12750";
			var testData = new flyui.bdgp.Service.TestData();
			var expectedImages = [
				testData.img_CG12750_1,
				testData.img_CG12750_2,
				testData.img_CG12750_3,
				testData.img_CG12750_4,
				testData.img_CG12750_5,
				testData.img_CG12750_6,
				testData.img_CG12750_7,
				testData.img_CG12750_8,
				testData.img_CG12750_9,
				testData.img_CG12750_10,
				testData.img_CG12750_11
			];
			flyui.bdgp.Service.ServiceTests.testFindImagesByAnyGeneLabel(geneLabel, expectedImages, endpointURL, this);
		}
		
	});
	return testCase;
};




// ************************************************************
// TEST DATA
// ************************************************************


/**
 * TODO doc me
 */
flyui.bdgp.Service.TestData = function(){
	
	this.img_CG10561 = new flyui.bdgp.Image();
	this.img_CG10561.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_13/insitu13361.jpe";
	this.img_CG10561.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_13/insitu13361.jpe";
//	this.img_CG10561.stage = "http://openflydata.org/id/bdgp/term/5885";
//	this.img_CG10561.terms = ["http://openflydata.org/id/bdgp/term/5885", "http://openflydata.org/id/bdgp/term/6005"];

//	this.term_5885 = new flyui.bdgp.Term();
//	this.term_5885.name = "stage13-16";
//	this.term_6005 = new flyui.bdgp.Term();
//	this.term_6005.name = "faint ubiquitous";
	
	this.img_CG12750_1 = new flyui.bdgp.Image();
	this.img_CG12750_1.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14276.jpe"; 
	this.img_CG12750_1.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14276.jpe";
//	this.img_img_CG12750_1.stage = "http://openflydata.org/id/bdgp/term/5884";
//	this.img_img_CG12750_1.terms = ["http://openflydata.org/id/bdgp/term/5884", 
//		"http://openflydata.org/id/bdgp/term/6005", 
//		"http://openflydata.org/id/bdgp/term/5981"];
	
//	this.term_5884 = new flyui.bdgp.Term();
//	this.term_5884.name = "stage11-12";
//	this.term_5981 = new flyui.bdgp.Term();
//	this.term_5981.name = "salivary gland primordium";
	
	this.img_CG12750_2 = new flyui.bdgp.Image();
	this.img_CG12750_2.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14277.jpe"; 
	this.img_CG12750_2.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14277.jpe";
//	this.img_img_CG12750_2.stage = "http://openflydata.org/id/bdgp/term/5884";
//	this.img_img_CG12750_2.terms = ["http://openflydata.org/id/bdgp/term/5884", 
//		"http://openflydata.org/id/bdgp/term/6005", 
//		"http://openflydata.org/id/bdgp/term/5981"];
	
	this.img_CG12750_3 = new flyui.bdgp.Image();
	this.img_CG12750_3.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14278.jpe"; 
	this.img_CG12750_3.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14278.jpe";
//	this.img_img_CG12750_3.stage = "http://openflydata.org/id/bdgp/term/5884";
//	this.img_img_CG12750_3.terms = ["http://openflydata.org/id/bdgp/term/5884", 
//		"http://openflydata.org/id/bdgp/term/6005", 
//		"http://openflydata.org/id/bdgp/term/5981"];
//	
	this.img_CG12750_4 = new flyui.bdgp.Image();
	this.img_CG12750_4.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14275.jpe"; 
	this.img_CG12750_4.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14275.jpe";
//	this.img_img_CG12750_4.stage = "http://openflydata.org/id/bdgp/term/5882";
//	this.img_img_CG12750_4.terms = ["http://openflydata.org/id/bdgp/term/5882", 
//		"http://openflydata.org/id/bdgp/term/43", 
//		"http://openflydata.org/id/bdgp/term/314",
//		"http://openflydata.org/id/bdgp/term/5904",
//		"http://openflydata.org/id/bdgp/term/5905"];
//	this.term_5882 = new flyui.bdgp.Term();
//	this.term_5882.name = "stage7-8";
//	this.term_43 = new flyui.bdgp.Term();
//	this.term_43.name = "ubiquitous";
//	this.term_314 = new flyui.bdgp.Term();
//	this.term_314.name = "procephalic ectoderm anlage";
//	this.term_5904 = new flyui.bdgp.Term();
//	this.term_5904.name = "ventral ectoderm primordium P2";
//	this.term_5905 = new flyui.bdgp.Term();
//	this.term_5905.name = "posterior endoderm primordium P2";

	this.img_CG12750_5 = new flyui.bdgp.Image();
	this.img_CG12750_5.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14273.jpe"; 
	this.img_CG12750_5.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14273.jpe";
//	this.img_img_CG12750_5.stage = "http://openflydata.org/id/bdgp/term/5882";
//	this.img_img_CG12750_5.terms = ["http://openflydata.org/id/bdgp/term/5882", 
//		"http://openflydata.org/id/bdgp/term/43", 
//		"http://openflydata.org/id/bdgp/term/314",
//		"http://openflydata.org/id/bdgp/term/5904",
//		"http://openflydata.org/id/bdgp/term/5905"];

	this.img_CG12750_6 = new flyui.bdgp.Image();
	this.img_CG12750_6.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14274.jpe"; 
	this.img_CG12750_6.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14274.jpe";
//	this.img_img_CG12750_6.stage = "http://openflydata.org/id/bdgp/term/5882";
//	this.img_img_CG12750_6.terms = ["http://openflydata.org/id/bdgp/term/5882", 
//		"http://openflydata.org/id/bdgp/term/43", 
//		"http://openflydata.org/id/bdgp/term/314",
//		"http://openflydata.org/id/bdgp/term/5904",
//		"http://openflydata.org/id/bdgp/term/5905"];
	
	this.img_CG12750_7 = new flyui.bdgp.Image();
	this.img_CG12750_7.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14279.jpe"; 
	this.img_CG12750_7.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14279.jpe";
//	this.img_img_CG12750_7.stage = "http://openflydata.org/id/bdgp/term/5885";
//	this.img_img_CG12750_7.terms = ["http://openflydata.org/id/bdgp/term/5885", 
//		"http://openflydata.org/id/bdgp/term/1758", 
//		"http://openflydata.org/id/bdgp/term/6005"];
//	this.term_6005 = new flyui.bdgp.Term();
//	this.term_6005.name = "embryonic salivary gland";
	
	this.img_CG12750_8 = new flyui.bdgp.Image();
	this.img_CG12750_8.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14280.jpe"; 
	this.img_CG12750_8.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14280.jpe";
//	this.img_img_CG12750_8.stage = "http://openflydata.org/id/bdgp/term/5885";
//	this.img_img_CG12750_8.terms = ["http://openflydata.org/id/bdgp/term/5885", 
//		"http://openflydata.org/id/bdgp/term/1758", 
//		"http://openflydata.org/id/bdgp/term/6005"];
	
	this.img_CG12750_9 = new flyui.bdgp.Image();
	this.img_CG12750_9.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14271.jpe"; 
	this.img_CG12750_9.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14271.jpe";
//	this.img_img_CG12750_9.stage = "http://openflydata.org/id/bdgp/term/5880";
//	this.img_img_CG12750_9.terms = ["http://openflydata.org/id/bdgp/term/5880", 
//		"http://openflydata.org/id/bdgp/term/53"];
//	this.term_53 = new flyui.bdgp.Term();
//	this.term_53.name = "maternal";
	
	this.img_CG12750_10 = new flyui.bdgp.Image();
	this.img_CG12750_10.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14270.jpe"; 
	this.img_CG12750_10.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14270.jpe";
//	this.img_img_CG12750_10.stage = "http://openflydata.org/id/bdgp/term/5880";
//	this.img_img_CG12750_10.terms = ["http://openflydata.org/id/bdgp/term/5880", 
//		"http://openflydata.org/id/bdgp/term/53"];
	
	this.img_CG12750_11 = new flyui.bdgp.Image();
	this.img_CG12750_11.fullImageURL = "http://www.fruitfly.org/insituimages/insitu_images/img_dir_14/insitu14272.jpe"; 
	this.img_CG12750_11.thumbnailURL = "http://www.fruitfly.org/insituimages/insitu_images/thumbnails/img_dir_14/insitu14272.jpe";
//	this.img_img_CG12750_11.stage = "http://openflydata.org/id/bdgp/term/5881";
//	this.img_img_CG12750_11.terms = ["http://openflydata.org/id/bdgp/term/5881", 
//		"http://openflydata.org/id/bdgp/term/43"];
	
	
	//	this.stage_5880 = new flyui.bdgp.Stage();
	//	this.stage_5880.name = "stage1-3";
	
	//	this.stage_5881 = new flyui.bdgp.Stage();
	//	this.stage_5882.name = "stage4-6";
	
	//	this.stage_5882 = new flyui.bdgp.Stage();
	//	this.stage_5882.name = "stage7-8";
	
	//	this.stage_5883 = new flyui.bdgp.Stage();
	//	this.stage_5883.name = "stage9-10";
	
	//	this.stage_5884 = new flyui.bdgp.Stage();
	//	this.stage_5884.name = "stage11-12";
	
	//	this.stage_5885 = new flyui.bdgp.Stage();
	//	this.stage_5885.name = "stage13-16";
	
}


/**
 * Compare two image objects.
 * @param {flyui.bdgp.Image} expected
 * @param {flyui.bdgp.Image} actual
 */
flyui.bdgp.Service.TestData.compareImages = function( expected, actual ) {
	
	log("test bdgp full image url", "test");
	assert.areEqual(expected.fullImageURL, actual.fullImageURL, "full image URL not expected");
	
	log("test bdgp thumbnail url", "test");
	assert.areEqual(expected.thumbnailURL, actual.thumbnailURL, "thumbnail URL not expected");
	
}



/**
 * TODO doc me
 */
flyui.bdgp.Service.TestData.compareImageArrays = function( expecteds, actuals ) {

	log("comparing image arrays", "test");

	assert.areEqual(expecteds.length, actuals.length, "number of images not as expected");
	
	var n = actuals.length;
	for (var i=0; i<n; i++) {
		var expected = expecteds[i];
		var found = false;
		for (var j=0; j<n; j++) {
			var image = actuals[j];
			if (image.fullImageURL == expected.fullImageURL) {
				found = true;
				flyui.bdgp.Service.TestData.compareImages(expected, image);
			}
		}
		assert.isTrue(found, "did not find expected image "+expected.fullImageURL);
	}
	
}


// ************************************************************
// TEST SUITE AND RUNNER
// ************************************************************


/** 
 * Construct a YUI test suite for the flyui.bdgp.Service module.
 * @param {String} endpointURL URL of service
 * @return a YUI test suite
 * @type YAHOO.tool.TestSuite
 */
flyui.bdgp.Service.TestSuite = function(endpointURL) {
	var suite = new YAHOO.tool.TestSuite("== flyui.bdgp.Service Test Suite ==");
	suite.add(flyui.bdgp.Service.ModuleTestCase());
//	suite.add(flyui.bdgp.Service.ImageTestCase());
	suite.add(flyui.bdgp.Service.ServiceTestCase(endpointURL));
	return suite;
}


/** 
 * Run the flyui.bdgp.Service test suite.
 * @param {String} endpointURL URL of service
 */
flyui.bdgp.Service.runTests = function(endpointURL) {
	YAHOO.log("flyui.bdgp.Service :: running tests", "test");
	YAHOO.tool.TestRunner.add(flyui.bdgp.Service.TestSuite(endpointURL));
	YAHOO.tool.TestRunner.run();
}
